package so.sao.shop.supplier.service.authorized.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.sao.shop.supplier.dao.authorized.PermissionDao;
import so.sao.shop.supplier.domain.authorized.Permission;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.service.authorized.PermissionService;
import so.sao.shop.supplier.util.Ognl;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionDao permissionDao;

	@Override
	public void insert(Permission permission) {
		permissionDao.insert(permission);
	}

	@Override
	public void startInsert(Permission permission) {
		//List<Permission> permissions = this.findAll();
		/*if(permissions.size() <= 0) {
			this.insert(permission);
		}*/
		/*for(Permission per:permissions) {
			if (permission.getUrl().equals(per.getUrl())) {
				return ;
			}
		}*/
		this.insert(permission);
	}
	
	@Override
	public Result<?> findAll() {
		List<Permission> permissions = permissionDao.findAll();
		return Result.success("查询成功!", permissions);
	}

	@Override
	public Result<?> findAllPermissionTree(Long userId,Long parentId,String flag) {
		List<Permission> permissions = null;
		if(parentId == null) {
			permissions = permissionDao.findMenuPermission(userId,null,null);
		} else {
			permissions = permissionDao.findAppointMenuPermission(userId,parentId);
		}
		Map<String,Object> permissionMap = findPermissionTree(permissions,null);
		return Result.success("查询成功!", permissionMap);
	}

	@Override
	public Result<?> findMenuPermission(Long userId,String flag){
		Long type = 1L;
		List<Permission> list = permissionDao.findMenuPermission(userId,type,null);
		if (list!=null && !list.isEmpty()){
			Map<String,Object> map = findPermissionTree(list,null);
			return Result.success("查询成功",map);
		}
		return Result.fail("没有数据记录");
	}

	@Override
	public Result<?> findButtonPermission(Long userId,String flag){
		Long type = null;
		List<Permission> list = permissionDao.findMenuPermission(userId,type,"flag");
		if (list!=null && !list.isEmpty()){
			Map<String,Object> map = findPermissionTree(list,flag);
			return Result.success("查询成功",map);
		}
		return Result.fail("没有数据记录");
	}
	@Override
	public Result<?> findPermissionByUser(Long userId){
		//查询所有type数据
		Long type = null;
		List<Permission> list = permissionDao.findMenuPermission(userId,type,null);
		if(list==null || list.isEmpty()){
			return Result.fail("没有数据记录");
		}
		//取出权限字段，重新封装list
		List<String> newList = new ArrayList<>();
		for (Permission permission :list){
			newList.add(permission.getRouteUrl());
		}
		Map<String,List<String>> map = new HashMap<>();
		map.put("permissions",newList);
		return Result.success("查询成功",map);
	}

	/**
	 * 查询权限树级结构公共方法
	 * @param
	 * @return
	 */
	public static Map<String,Object> findPermissionTree(List<Permission> allList,String flag){
		//根据ID分成单条,根据PID分组
		Map<Long,Permission> map = new HashMap<>();
		Map<Long,List<Permission>> pmap = new HashMap<>();
		for(Permission per:allList){
			//根据id拆分成单条
			map.put(per.getId(),per);
			//根据PID分组
			if(Ognl.isNull(pmap.get(per.getParentId()))){
				List<Permission> list = new ArrayList<>();
				list.add(per);
				pmap.put(per.getParentId(),list);
			}else {
				List<Permission> list = pmap.get(per.getParentId());
				list.add(per);
				pmap.put(per.getParentId(),list);
			}
		}
		//取出pid为0的id
		List<Permission> topList = pmap.get(0L) == null ? new ArrayList<Permission>() : pmap.get(0L);
		//封装返回数据
		Map<String,Object> resultMap = new LinkedHashMap<>();
		List<Map<String,Object>> childrenList = new ArrayList<Map<String,Object>>();
		for (Permission pm:topList){
			Map<String,Object> children= nodeTree(map,pmap,pm.getId(),flag);
			childrenList.add(children);
		}
		resultMap.put("permissions",childrenList);
		return resultMap;
	}

	/**
	 * 根据id查询其子节点集
	 * @param map
	 * @param pMap
	 * @param id
	 * @return
	 */
	private static Map<String,Object> nodeTree(Map<Long ,Permission> map, Map<Long ,List<Permission>> pMap , Long id,String flag ){
		Map<String,Object> resultMap = new LinkedHashMap<>();
		//根据id获取节点对象
		Permission pem= map.get(id);
		//封装对象
		//resultMap = BeanUtils.copyObjectToMap(pem);
		resultMap.put("id",pem.getId());
		resultMap.put("label",pem.getName());
		resultMap.put("description",pem.getDescription());
		resultMap.put("index",pem.getRouteUrl());
		resultMap.put("apiUrl",pem.getApiUrl());
		resultMap.put("code",pem.getCode());
		resultMap.put("type",pem.getType());
		resultMap.put("sort",pem.getSort());
		resultMap.put("icon",pem.getIcon());
		resultMap.put("parentId",pem.getParentId());
		if("1".equals(flag)){
			resultMap.put("disabled",true);
		}
		//查询该id下的所有子节点
		List<Permission> list= pMap.get(id);
		if (Ognl.isNull(list)){
			return resultMap;
		}
		//子节点集合
		List<Map<String,Object>> childrenList = new ArrayList<>();
		for (Permission per:list) {
			Map<String,Object> ccvo = nodeTree(map,pMap,per.getId(),flag);
			childrenList.add(ccvo);
		}
		resultMap.put("children",childrenList);
		return resultMap;
	}

	@Override
	public Result<?> selectRolePermission(Long loginUserId,List<Long> roleIds) {
		List<Permission> permissions = permissionDao.findRoleAllPermission(loginUserId, null,roleIds);
		return Result.success("查询成功!", permissions);
	}

}
